package edu.polsl.bio.str.node.service;

import edu.polsl.bio.str.common.exception.NodeException;
import edu.polsl.bio.str.node.logic.NodeContext;
import edu.polsl.bio.str.node.logic.NodeControlBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * //todo class description
 * <p/>
 * Created on: Jun 23, 2011 2:59:35 PM <br/>
 * <a href="http://www.polsl.pl">www.polsl.pl</a>
 *
 * @author lukasz.olczak@polsl.pl
 */
public class WebControlServiceImpl implements WebControlService {
  /**
   * slf4j Logger.
   */
  private static final Logger log = LoggerFactory.getLogger(WebControlServiceImpl.class.getName());

  private NodeControlBean nodeControlBean;

  @Override
  public NodeContext getNodeContext() throws NodeException {
    try {
      return nodeControlBean.getNodeContext();
    } catch (Exception e) {
      log.error("Error while geting node context ", e);
      throw createNodeException(e);
    }
  }

  @Override
  public long attachNode(String managerIp, String nodeIp) throws NodeException {
    try {
      return nodeControlBean.attachNode(managerIp, nodeIp);
    } catch (Exception e) {
      log.error("Error while attaching to manager " + managerIp, e);
      throw createNodeException(e);
    }
  }

  @Override
  public void detachNode() throws NodeException {
    try {
      nodeControlBean.detachNode();
    } catch (Exception e) {
      log.error("Error while detaching from manager", e);
      throw createNodeException(e);
    }
  }

  protected NodeException createNodeException(Exception e) {
    return new NodeException("Exception with msg: \'" + e.getMessage() + "\' occurred on server side");
  }

  public void setNodeControlBean(NodeControlBean nodeControlBean) {
    this.nodeControlBean = nodeControlBean;
  }
}
